package leetcode_day._2021._202111;

import java.util.HashMap;
import java.util.Map;

/**
 * @author yzh
 * @version 1.0
 * @date 2021/11/8 9:17
 * 猜数字游戏
 * 算法：哈希表  数组
 * 因为是数字，所以可以使用数组来存储
 */
public class _08_299 {
    // 1123   0111
    public static void main(String[] args) {
        System.out.println(new _08_299().getHint_perfect("1123", "0111"));
    }

    public String getHint(String secret, String guess) {
        int len = secret.length();
        Map<Character, Integer> map = new HashMap<>(16);
        int A = 0;
        for (int i = 0; i < secret.length(); i++) {
            char cur = secret.charAt(i);
            if (secret.charAt(i) == guess.charAt(i)) ++A;
            else map.put(cur, map.getOrDefault(cur, 0) + 1);
        }
        int B = 0;
        for (int i = 0; i < len; i++) {
            char cur = guess.charAt(i);
            if (cur != secret.charAt(i) && map.containsKey(cur)) {
                ++B;
                int num = map.get(cur);
                if (--num == 0) map.remove(cur);
                else map.put(cur, num);
            }
        }
        return A + "A" + B + 'B';
    }

    public String getHint_perfect(String secret, String guess) {
        int[] a = new int[10];
        int[] b = new int[10];
        int A = 0;
        for (int i = 0; i < secret.length(); i++) {
            if (secret.charAt(i) == guess.charAt(i)) {
                ++A;
                continue;
            }
            a[secret.charAt(i) - '0']++;
            b[guess.charAt(i) - '0'] ++;
        }
        int B = 0;
        for (int i = 0; i <= 9; i++) B += Math.min(a[i], b[i]);
        return A + "A" + B + 'B';
    }

}
